<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { login } from '../api/login.js'
import { CONFIG } from '../config/index.js'
import { useRouter } from 'vue-router'

const loginInfo = reactive({
  username: "",
  password: "",
})

const router = useRouter()
const loginref = ref()
const loading = ref(false)

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名至少3个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6个字符', trigger: 'blur' }
  ],
})

// Improved login function with error handling
const submitForm = async () => {
  if (loading.value) return // Prevent multiple submissions
  
  try {
    loading.value = true
    const response = await login(loginInfo.username, loginInfo.password)
    
    if (response.data.status === 200) {
      // Store token
      const token = response.data.token 
      window.localStorage.setItem(CONFIG.TOKEN_NAME, token)
      
      ElMessage.success(response.data.message)
      router.replace("/")
    } else {
      ElMessage.error(response.data.message || '登录失败')
    }
  } catch (error) {
    ElMessage.error('网络错误，请稍后重试')
  } finally {
    loading.value = false
  }
}

const loginButtonDisabled = ref(true)

// Validate form on input changes
watch([() => loginInfo.username, () => loginInfo.password], () => {
  if (loginref.value) {
    loginref.value.validate((valid) => {
      loginButtonDisabled.value = !valid
    })
  }
})
</script>

<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2>云原生k8s多集群管理平台</h2>
        <p class="login-subtitle">K8s Resource Management System</p>
      </div>
      
      <el-form
        ref="loginref"
        :model="loginInfo"
        :rules="rules"
        :validate-on-rule-change="false"
        @keyup.enter="submitForm"
        class="login-form"
      >
        <!-- Username Input -->
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User" 
            v-model="loginInfo.username"
            size="large"
            autocomplete="username"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>

        <!-- Password Input -->
        <el-form-item prop="password">
          <el-input 
            :prefix-icon="Lock" 
            size="large"
            show-password 
            v-model="loginInfo.password" 
            type="password" 
            autocomplete="current-password"
            placeholder="请输入密码"
          />
        </el-form-item>

        <!-- Login Button -->
        <el-form-item>
          <el-button 
            style="margin: 10px auto 10px auto;"
            class="login-button"
            :loading="loading"
            :disabled="loginButtonDisabled" 
            type="primary" 
            size="large" 
            @click="submitForm"
            block
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #4796f1 0%, #a285c0 100%);
}

.login-card {
  width: 100%;
  max-width: 450px;
  padding: 40px 30px;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.login-subtitle {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.login-form {
  width: 100%;
}

.login-button {
  margin-top: 10px;
}
</style>
